package com.hh.flow.controller;

import com.hh.common.utils.ExceptionUtil;
import com.hh.common.utils.MessageUtils;
import com.hh.flow.domain.FlowInstance;
import com.hh.flow.service.IFlowInstanceService;
import com.hh.log.annotaion.Log;
import com.hh.log.enums.BusinessType;
import com.hh.mybatis.controller.BaseController;
import com.hh.mybatis.domain.R;
import com.hh.mybatis.domain.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 流程实例Controller
 *
 * @author hh
 * @date 2023-04-18
 */
@Api(value = "流程实例控制器", tags = {"流程实例管理"})
@Validated
@RestController
@RequestMapping("/flow/instance")
public class FlowInstanceController extends BaseController<FlowInstance>
{
    @Resource
    private IFlowInstanceService flowInstanceService;

    @Override
    public IFlowInstanceService getBaseService()
    {
        return flowInstanceService;
    }

    /**
     * 分页查询流程实例列表
     */
    @PreAuthorize("@ss.hasPermi('flow:instance:list')")
    @GetMapping("/list")
    public TableDataInfo<FlowInstance> list(FlowInstance flowInstance)
    {
        try {
            startPage();
            List<FlowInstance> list = flowInstanceService.getList(flowInstance);
            return getDataTable(list);
        } catch (Exception e) {
            String msg = MessageUtils.message("controller.page");
            logger.error(msg, e);
            return TableDataInfo.fail(ExceptionUtil.handleMsg(msg, e));
        }
    }

    /**
     * 办理
     */
    @ApiOperation("办理")
    @PreAuthorize("@ss.hasPermi('flow:instance:handle')")
    @Log(title = "流程实例", businessType = BusinessType.OTHER)
    @GetMapping("/handle")
    public R handle(Long id, String conditionValue
            , String message)
    {
        try {
            return R.ok(flowInstanceService.handle(id, conditionValue, message));
        } catch (Exception e) {
            String msg = MessageUtils.message("controller.handle");
            logger.error(msg, e);
            return R.fail(ExceptionUtil.handleMsg(msg, e));
        }
    }


    /**
     * 获取流程实例详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:instance:query')")
    @GetMapping(value = "/{id}")
    public R<FlowInstance> getInfo(@PathVariable("id") Long id)
    {
        return getBaseInfo(id);
    }

    /**
     * 新增流程实例
     */
    @PreAuthorize("@ss.hasPermi('flow:instance:add')")
    @Log(title = "流程实例", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FlowInstance flowInstance)
    {
        return baseAdd(flowInstance);
    }

    /**
     * 修改流程实例
     */
    @PreAuthorize("@ss.hasPermi('flow:instance:edit')")
    @Log(title = "流程实例", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody FlowInstance flowInstance)
    {
        return baseEdit(flowInstance);
    }

    /**
     * 删除流程实例
     */
    @PreAuthorize("@ss.hasPermi('flow:instance:remove')")
    @Log(title = "流程实例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable List<Long> ids)
    {
        return baserRemove(ids);
    }
}
